package com.woniuxy.meal.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.meal.common.ConstantData;
import com.woniuxy.meal.dao.DietTimeConfigMapper;
import com.woniuxy.meal.entity.DietTimeConfig;
import com.woniuxy.meal.entity.StoreInfo;
import com.woniuxy.meal.exception.userCartException.UserCartException;
import com.woniuxy.meal.exception.userCartException.UserCartExceptionCode;
import com.woniuxy.meal.service.DietTimeConfigService;
import com.woniuxy.meal.service.FoodVarietyService;
import com.woniuxy.meal.service.StoreInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sound.midi.Track;
import java.time.LocalTime;
import java.util.List;

@Component
public class TimeUtil {

    @Autowired
    private DietTimeConfigMapper dietTimeConfigMapper;


    public boolean compareOpenTime(StoreInfo storeInfo){

        LocalTime startTime = storeInfo.getStartTime();
        LocalTime endTime = storeInfo.getEndTime();
        LocalTime now = LocalTime.now();
        boolean after = now.isAfter(startTime);
        boolean before = now.isBefore(endTime);
        if (!after||!before){
            throw new UserCartException(UserCartExceptionCode.STORE_STATE_CLOSE);
        }
        return true;
    }


    public boolean compareDeliveryTime(Long storeId){
        LocalTime now = LocalTime.now();
        QueryWrapper<DietTimeConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("storeId",storeId);
        List<DietTimeConfig> dietTimeConfigs = dietTimeConfigMapper.selectList(queryWrapper);
        for (DietTimeConfig dietTimeConfig : dietTimeConfigs) {
            LocalTime stime = dietTimeConfig.getStartTime();
            LocalTime etime = dietTimeConfig.getEndTime();
            boolean bool1 = now.isAfter(stime);
            boolean bool2 = now.isBefore(etime);
            if (bool1 && bool2) {
                return true;
            }
        }
        throw new UserCartException(UserCartExceptionCode.NOT_IN_DELIVERY_TIME);
    }
}
